/*
 * ========================
 * Author : David Albertson
 * Date : October 5, 2011
 * Project : andro-repo
 * ========================
 */

package com.andengine.lwtemplate;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.extension.physics.box2d.util.Vector2Pool;
import org.anddev.andengine.extension.ui.livewallpaper.BaseLiveWallpaperService;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.sensor.accelerometer.AccelerometerData;
import org.anddev.andengine.sensor.accelerometer.IAccelerometerListener;

import android.content.SharedPreferences;
import android.hardware.SensorManager;
import android.util.Log;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class LiveWallpaper extends BaseLiveWallpaperService implements SharedPreferences.OnSharedPreferenceChangeListener, IAccelerometerListener
{
	// ===========================================================
	// Constants
	// ===========================================================
	
	public static final String SHARED_PREFS_NAME = "livewallpapertemplatesettings";
	private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(2, 0.1f, 0f);
	//Camera Constants
	// ASUS TRANFORMER : 1280 x 800
	private static final int CAMERA_WIDTH = 1280;
	private static final int CAMERA_HEIGHT = 800;

	// ===========================================================
	// Fields
	// ===========================================================
	
	//Shared Preferences
	private SharedPreferences mSharedPreferences;

	private PhysicsWorld mPhysicsWorld;

	private BitmapTextureAtlas mBitmapTextureAtlas;

	private TextureRegion mFaceTextureRegion;
	
	private Sprite asa, asa11;
	Scene scene;
	private BitmapTextureAtlas mBitmapTextureAtlas1;
	private TextureRegion mbgTextureRegion;


	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public org.anddev.andengine.engine.Engine onLoadEngine()
	{
		final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
		engineOptions.getTouchOptions().setRunOnUpdateThread(true);
		return new org.anddev.andengine.engine.Engine(engineOptions);
	}

	@Override
	public void onLoadResources()
	{
		/* Textures. */
		this.mBitmapTextureAtlas = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mBitmapTextureAtlas1 = new BitmapTextureAtlas(2048, 2048, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		/* TextureRegions. */
		this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "canadien.png", 0, 0);
		this.mbgTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas1, this, "bg.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
		this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas1);
	}

	@Override
	public Scene onLoadScene()
	{
		scene = new Scene(1);
		scene.setBackground(new ColorBackground(0f, 0.36f, 0.72f));
		this.enableAccelerometerSensor(this);
		this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);

		final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
		final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
		final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
		final Shape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT);

		final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
		PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
		PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
		PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
		PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);

		scene.attachChild(ground);
		scene.attachChild(roof);
		scene.attachChild(left);
		scene.attachChild(right);

		scene.registerUpdateHandler(this.mPhysicsWorld);
		asa11 = addFace1(0,0);
		asa = addFace(CAMERA_WIDTH/2 - 512/2, CAMERA_HEIGHT/2 - 512/2);
		
		
		
		return scene;
	}

	@Override
	public void onLoadComplete()
	{

	}
	
	@Override
	protected void onTap(final int pX, final int pY)
	{
		
	}
	
	@Override
	public void onSharedPreferenceChanged(SharedPreferences pSharedPrefs, String pKey)
	{
		
	}

	@Override
	public void onPauseGame() {
		// TODO Auto-generated method stub
		
	}

	public Sprite addFace(int a, int b){
		final Sprite face;
		final Body body;
		
		face = new Sprite(a, b, this.mFaceTextureRegion);
		body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
		

		
		face.setScale(0.8f);
		this.scene.attachChild(face);
		//this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
		
		return face;
	}
	
	public Sprite addFace1(int a, int b){
		final Sprite face;
		final Body body;
		
		face = new Sprite(a, b, this.mbgTextureRegion);
		body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
		

		
		//face.setScale(0.8f);
		this.scene.attachChild(face);
		//this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
		
		return face;
	}
	
	@Override
	public void onResumeGame() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUnloadResources() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {
		final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX(), pAccelerometerData.getY());
		this.mPhysicsWorld.setGravity(gravity);
		Vector2Pool.recycle(gravity);
		
		double y = pAccelerometerData.getY();
		double x = pAccelerometerData.getX();
		
	
		double tan1 = (float)Math.atan(x/y);;
		float angle = (float)Math.toDegrees(-tan1);


		
		Log.v("Rotation", String.valueOf(angle));
		Log.v("XY", "X : " + String.valueOf(x) + ", y : " + String.valueOf(y));
		
		if(pAccelerometerData.getZ() < 8){
			if(x > 0 && y < 0){
				asa.setRotation(-90-(90-angle));
			}
			if(x < 0 && y < 0){
				asa.setRotation(90+(90+angle));
			}
			else if((x<=0 && y>=0)||(x>=0 && y >=0)){
				asa.setRotation(angle);
			}
		}
		
		//Log.v("Accel", "X : " + String.valueOf(pAccelerometerData.getX()) + ", Y : " + String.valueOf(pAccelerometerData.getY()) + ", Z : " + String.valueOf(pAccelerometerData.getZ()));
		
	}

	
	// ===========================================================
	// Methods
	// ===========================================================	

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
